<?php namespace Admin\Model;
use Hdphp\Model\Model;
class Brand extends Model{
	protected $table = "brand";
	//验证
	protected $validate=array(
		array('bname','required','品牌名称不能为空',3,3),
		array('bname','maxlen:30','品牌名称不能超过30个字符',3,3),
		array('sort','num:0,67295','数字超出范围',3,3),
		array('hot','required','请选择是否热门',3,3)
	);
	protected $auto = array(
		array('logo','image','method',3,3)
	);
	//添加
	public function store(){
		if($this->create()) {
			if($_FILES['logo']['error'] !=4){
				//如果有错误
				//就报错
				if($error = Upload::getError()){
					$this->error=$error;
					return false;
				}
			}
			$this->add();
			return true;
		}
		return false;
	}
	//自定义logo上传方法
	public function image($img){
		if($img) return $img;
		//如果有旧图片地址(点击后地址传到隐藏域)
		if($oldLogo = Q('post.logo')){
			return $oldLogo;
		}
		$uploadDir = 'Upload/'.date('y/m/d');
		$file = Upload::type('png,jpg,gif')->path($uploadDir)->size(2000000)->make();
		if($file){
			// $path = Image::thumb($file[0]['path'], $uploadDir.'logo'.$file[0]['basename'], 120,50, 2);
			return $file[0]['path'];
		}
		return '';
	}
	//删除
	public function del($bid){
		$this->where("bid={$bid}")->delete();
		return true;
	}
	//编辑
	public function edit($bid){
		if(!$this->create()) return false;
		//如果有上传图片
			if($_FILES['logo']['error'] !=4){
				//如果有错误
				//就报错
				if($error = Upload::getError()){
					$this->error=$error;
					return false;
				}
			}
		$this->where("bid={$bid}")->save();
		return true;
	}











}
?>